package com.ruoyi.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.domain.PhgRequirement;
import com.ruoyi.mes.service.IPhgRequirementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购需求Controller
 * 
 * @author CFL
 * @date 2024-04-28
 */
@RestController
@RequestMapping("/mes/requirement")
public class PhgRequirementController extends BaseController
{
    @Autowired
    private IPhgRequirementService phgRequirementService;

    /**
     * 查询采购需求列表
     */
    @PreAuthorize("@ss.hasPermi('mes:requirement:list')")
    @GetMapping("/list")
    public TableDataInfo list(PhgRequirement phgRequirement)
    {
        startPage();
        List<PhgRequirement> list = phgRequirementService.selectPhgRequirementList(phgRequirement);
        return getDataTable(list);
    }

    /**
     * 导出采购需求列表
     */
    @PreAuthorize("@ss.hasPermi('mes:requirement:export')")
    @Log(title = "采购需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PhgRequirement phgRequirement)
    {
        List<PhgRequirement> list = phgRequirementService.selectPhgRequirementList(phgRequirement);
        ExcelUtil<PhgRequirement> util = new ExcelUtil<PhgRequirement>(PhgRequirement.class);
        util.exportExcel(response, list, "采购需求数据");
    }

    /**
     * 获取采购需求详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:requirement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(phgRequirementService.selectPhgRequirementById(id));
    }

    /**
     * 获取产品物料信息
     */
    @PreAuthorize("@ss.hasPermi('mes:requirement:query')")
    @GetMapping(value = "/product-material/{proId}")
    public List<PhgRequirement> getProductInfo(@PathVariable("proId") Long proId)
    {
        return phgRequirementService.selectPhgRequirementByProId(proId);
    }

    /**
     * 新增采购需求
     */
    @PreAuthorize("@ss.hasPermi('mes:requirement:add')")
    @Log(title = "采购需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PhgRequirement phgRequirement)
    {
        return toAjax(phgRequirementService.insertPhgRequirement(phgRequirement));
    }

    /**
     * 修改-新增采购需求
     */
    @PreAuthorize("@ss.hasPermi('mes:requirement:add')")
    @Log(title = "采购需求", businessType = BusinessType.INSERT)
    @PostMapping("/purchase")
    public AjaxResult addPurchase(@RequestBody List<PhgRequirement> phgRequirementList)
    {
        return phgRequirementService.insertRequirement(phgRequirementList);
    }

    /**
     * 修改采购需求
     */
    @PreAuthorize("@ss.hasPermi('mes:requirement:edit')")
    @Log(title = "采购需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PhgRequirement phgRequirement)
    {
        return toAjax(phgRequirementService.updatePhgRequirement(phgRequirement));
    }

    /**
     * 删除采购需求
     */
    @PreAuthorize("@ss.hasPermi('mes:requirement:remove')")
    @Log(title = "采购需求", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(phgRequirementService.deletePhgRequirementByIds(ids));
    }
}
